﻿Public Class frmTasksModify

    Public mID As Integer

    Public Function Initialize(ByVal inID As Integer) As Boolean
        Try
            Me.Text = "Tasks"
            mID = inID

            cboActivities.Load("Select * From Activities", "Name", gConnectionStringAgenda.ToString)
            cboUS.Load("Select * From UserStories", "Name", gConnectionStringAgenda.ToString)
            Dim listStatus As New List(Of Base.GlobalConst)
            listStatus.Add(TASK_STATUS_PLANNED)
            listStatus.Add(TASK_STATUS_ONGOING)
            listStatus.Add(TASK_STATUS_ONHOLD)
            listStatus.Add(TASK_STATUS_DONE)
            listStatus.Add(TASK_STATUS_CANCELLED)
            listStatus.Add(TASK_STATUS_NEXT)
            cboStatus.DataSource = listStatus
            cboStatus.ValueMember = "NUMBER"
            cboStatus.DisplayMember = "NAME"
            listStatus = Nothing

            AddHandler cboActivities.OnInsert, AddressOf InterfaceManager.Activities_OnInsert
            AddHandler cboActivities.OnModify, AddressOf InterfaceManager.Activities_OnModify
            AddHandler cboUS.OnInsert, AddressOf InterfaceManager.UserStories_OnInsert
            AddHandler cboUS.OnModify, AddressOf InterfaceManager.UserStories_OnModify


            If inID > 0 Then
                cmdSave.Text &= "Modify"
                If Not LoadData() Then
                    MsgBox("Impossible to load data", vbExclamation, Me.Text)
                    Return False
                End If
            Else
                cmdSave.Text &= "Insert"
            End If

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "LoadData"

    Private Function LoadData() As Boolean
        Try
            Dim oTTasks As New Data.TTasks
            If oTTasks.LoadData(mID) Then
                txtDescription.Text = oTTasks.mDescription
                cboActivities.SelectedValue = oTTasks.mActivitiesID
                cboUS.SelectedValue = oTTasks.mUserStoriesID
                cboStatus.SelectedValue = oTTasks.mStatus
                txtExpectedHours.Text = oTTasks.mExpectedHours
                
            Else
                Return False
            End If
            oTTasks = Nothing
            Return True
        Catch ex As Exception
            Base.ShowError.Show("LoadData", Me.Name, "LoadData", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Comandi"

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            If IsComplete() Then
                Dim oTTasks As New Data.TTasks
                oTTasks.LoadData(mID)
                oTTasks.mDescription = txtDescription.Text
                oTTasks.mActivitiesID = cboActivities.SelectedValue
                oTTasks.mUserStoriesID = cboUS.SelectedValue
                oTTasks.mStatus = cboStatus.SelectedValue
                oTTasks.mExpectedHours = txtExpectedHours.GetDecimal

                Dim blnGoOn As Integer = 0
                If mID > 0 Then
                    blnGoOn = oTTasks.Modify
                Else
                    mID = oTTasks.Insert
                    blnGoOn = CBool(mID)
                End If

                If blnGoOn Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                Else
                    Me.DialogResult = Windows.Forms.DialogResult.Cancel
                End If

                oTTasks = Nothing
            End If
        Catch ex As Exception
            Base.ShowError.Show("Insert", Me.Name, "cmdSave_Click", ex)
        End Try
    End Sub

#End Region

#Region "Other functions"

    Private Function IsComplete() As Boolean
        Try
            If txtDescription.Text = "" Then
                MsgBox("Insert description.", vbExclamation, Me.Name)
                txtDescription.Focus()
                Return False
            End If
            If Not cboUS.IsItemSelected Then
                MsgBox("Select user story.", vbExclamation, Me.Name)
                cboUS.Focus()
                Return False
            End If
            If txtExpectedHours.Text = "" Then
                MsgBox("Insert hours.", vbExclamation, Me.Name)
                txtExpectedHours.Focus()
                Return False
            End If
            
            Return True
        Catch ex As Exception
            Base.ShowError.Show("Insert", Me.Name, "IsComplete", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Form events"

    Private Sub me_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
        End If
    End Sub

#End Region

End Class